package seiacpjava_a2.utilities;

import java.util.ArrayList;
import java.util.List;

public class NumberOfAccessCalculator {

	List<Integer> foundAccesses;
	List<Integer> notFoundAccesses;
	public NumberOfAccessCalculator(){
		this.foundAccesses = new ArrayList<>();
		this.notFoundAccesses = new ArrayList<>();
	}

	public void addNbrAccess(int nbrAccess,boolean found) {
		if (found)
			addFoundNbrAccess(nbrAccess);
		else
			addNotFoundNbrAccess(nbrAccess);
	}
	
	public void addFoundNbrAccess(int nbrAccess) {
		this.foundAccesses.add(nbrAccess);
	}
	
	public void addNotFoundNbrAccess(int nbrAccess) {
		this.notFoundAccesses.add(nbrAccess);
	}
	
	public double calculateFoundAverageNbrAccesses() {
		return calculateAverageNbrAccesses(foundAccesses);
	}
	
	public double calculateNotFoundAverageNbrAccesses() {
		return calculateAverageNbrAccesses(notFoundAccesses);
	}
	
	public int nbrOfFoundSearches() {
		return this.foundAccesses.size();
	}
	
	public int nbrOfMissedSearches() {
		return this.notFoundAccesses.size();
	}
	
	private double calculateAverageNbrAccesses(List<Integer> nbrAccesses) {
		if(nbrAccesses.size() == 0)
			return 0;
		
		double total = 0;
		for (Integer item : nbrAccesses) {
			total += (item != null)? item : 0; 
		}
		return total/nbrAccesses.size(); 
	}
	
	
}
